package edu.ccut.saturn.component.utils.logger;

import java.io.PrintWriter;
import java.io.StringWriter;

import edu.ccut.saturn.component.IDebugComponentInfo;
import edu.ccut.saturn.component.IDebugThread;
import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.ILoggerListener;
import edu.ccut.saturn.component.SaturnDebugThreadsManager;
import edu.ccut.saturn.component.impl.SaturnDebugLogMessage;

/**
 * 为Debug记录日志信息实现了ILoggerListener定义的日志操作的接口
 * 
 * @author 温建
 * 
 * @version 1.0 2008.09.09
 * 
 */
public class DebugLoggerListener implements ILoggerListener {

	private SaturnDebugThreadsManager saturnDebugThreadsManager = SaturnDebugThreadsManager
			.getInstance();

	public DebugLoggerListener() {
	}

	public void debug(String key, String message, Throwable throwable) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_DEBUG, message, throwable);
	}

	public void debug(String key, String message) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_DEBUG, message, null);
	}

	public void error(String key, String message, Throwable throwable) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_ERROR, message, throwable);
	}

	public void error(String key, String message) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_ERROR, message, null);
	}

	public void fatal(String key, String message, Throwable throwable) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_FATAL, message, throwable);
	}

	public void fatal(String key, String message) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_FATAL, message, null);
	}

	public String getLevel(String key) {
		return ILogger.LOGGER_LEVLE_INFO;
	}

	public void info(String key, String message) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_INFO, message, null);
	}

	public void info(String key, String message, Throwable throwable) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_INFO, message, throwable);
	}

	public void setLevel(String key, String level) {
	}

	public void warn(String key, String message, Throwable throwable) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_WARN, message, throwable);
	}

	public void warn(String key, String message) {
		this.writeMessage(key, ILogger.LOGGER_LEVLE_WARN, message, null);
	}

	private void writeMessage(String key, String level, String message,
			Throwable throwable) {
		
		IDebugThread debugThread = this.getDebugThread();

		if (debugThread != null) {
			IDebugComponentInfo debugComponentInfo = debugThread
					.getRuntimeDebugComponetInfo();

			if (debugComponentInfo != null) {
				SaturnDebugLogMessage logMessage = new SaturnDebugLogMessage();

				logMessage.setKey(key);
				logMessage.setLevel(level);
				logMessage.setMessage(message);

				if (throwable != null) {
					StringWriter writer = new StringWriter();
					PrintWriter printWriter = new PrintWriter(writer);
					throwable.printStackTrace(printWriter);
					printWriter.close();

					logMessage.setException(writer.toString());
				}
				
				debugComponentInfo.addLogMessage(logMessage);
			}
		}
	}

	private IDebugThread getDebugThread() {
		if (saturnDebugThreadsManager.isStart()) {
			return saturnDebugThreadsManager.getDebugThread(Thread
					.currentThread().getName());
		}

		return null;
	}
}
